package com.iplay.thezen.singleton;

import cn.hutool.core.util.RandomUtil;

import java.util.ArrayList;
import java.util.Random;

/**
 * @author dangwu
 * @since 2021/9/6 3:07 下午
 */
public class Emperor {
    //定义最多能产生的实例数量
    private static int maxNumOfEmperor = 2;
    //每个皇帝都有名字，使用一个ArrayList来容纳每个对象的私有属性
    private static ArrayList<String> nameList = new ArrayList<>();
    //定义一个列表，容纳所有的皇帝实例
    private static ArrayList<Emperor> emperorList = new ArrayList<>();
    //当前皇帝序列号
    private static int countNumOfEmperor = 0;
    static {
        for (int i = 0; i < maxNumOfEmperor; i++) {
            emperorList.add(new Emperor("皇"+(i+1)+"帝"));
        }
    }
    private Emperor(){}
    private Emperor(String name){
        nameList.add(name);
    }

    public static Emperor getInstance()
    {
        countNumOfEmperor = new Random().nextInt(maxNumOfEmperor);
        return emperorList.get(countNumOfEmperor);
    }
    public void say(){
        System.out.println(nameList.get(countNumOfEmperor));
    }
}
